﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace FrameWork
{
    public partial class ResourceModule : BaseModule
    {
        private class Loader
        {
            private Dictionary<int, BaseTask> _taskPool;

            private int _serializeId;

            public Loader()
            {
                _taskPool = new Dictionary<int, BaseTask>();
            }

            private int GetNextSerializeId()
            {
                if (_serializeId == int.MaxValue)
                    _serializeId = 0;
                _serializeId++;
                return _serializeId;
            }

            public int LoadAsset<T>(string uniqueKey, LoadAssetCallBack callBack, object userData = null)
            {
                LoadAssetTask task = new LoadAssetTask(uniqueKey, callBack, userData);
                task.Start<T>();
                int id = GetNextSerializeId();
                _taskPool.Add(id, task);
                return id;
            }

            public void Update(float deltaTime, float realDeltaTime)
            {
                foreach (var pair in _taskPool)
                {
                    var curTask = pair.Value;
                    curTask.Update(deltaTime, realDeltaTime);
                }
            }

            public void UnloadAsset(int serializeId)
            {
                if (serializeId < 0)
                    throw new InvalidCastException($"serializeId---{serializeId} is invaild!");

                if (_taskPool.TryGetValue(serializeId, out var loadTask))
                {
                    _taskPool.Remove(serializeId);
                    loadTask.Release();
                }
            }

            //场景和资源会有相同的key加载到字典中，需要处理
            public int LoadScene(string sceneKey, LoadSceneCallBack sceneCallBack, LoadSceneMode sceneLoadMode)
            {
                LoadSceneTask sceneTask = new LoadSceneTask(sceneKey, sceneCallBack);
                sceneTask.Start(sceneLoadMode);
                int id = GetNextSerializeId();
                _taskPool.Add(id, sceneTask);
                return id;
            }

            /// <summary>
            /// 场景卸载
            /// </summary>
            public void UnLoadScene(int serializeId, UnLoadSceneCallBack unLoadSceneCallBack)
            {
                if (_taskPool.TryGetValue(serializeId, out BaseTask baseTask))
                {
                    if (baseTask is LoadSceneTask sceneTask)
                    {
                        var originHandle = sceneTask.Handle;
                        _taskPool.Remove(serializeId);
                        UnLoadSceneTask unloadTask = new UnLoadSceneTask(originHandle, unLoadSceneCallBack);
                        unloadTask.Start();
                        int id = GetNextSerializeId();
                        _taskPool.Add(id, unloadTask);
                    }
                }
            }

            public void ShutDown()
            {
                _taskPool.Clear();
            }
        }
    }
}